/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package qs.crm.modules.client.ordersLine.service.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import qs.crm.modules.client.ordersLine.domain.OrdersLine;
import qs.crm.modules.client.ordersLine.repository.OrdersLineRepository;
import qs.crm.modules.client.ordersLine.service.OrdersLineService;
import qs.crm.modules.client.ordersLine.service.dto.OrdersLineDto;
import qs.crm.modules.client.ordersLine.service.dto.OrdersLineQueryCriteria;
import qs.crm.modules.client.ordersLine.service.mapstruct.OrdersLineMapper;
import qs.crm.utils.FileUtil;
import qs.crm.utils.PageUtil;
import qs.crm.utils.QueryHelp;
import qs.crm.utils.ValidationUtil;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author tong
* @date 2023-06-01
**/
@Service
@RequiredArgsConstructor
public class OrdersLineServiceImpl implements OrdersLineService {

    private final OrdersLineRepository ordersLineRepository;
    private final OrdersLineMapper ordersLineMapper;

    @Override
    public Map<String,Object> queryAll(OrdersLineQueryCriteria criteria, Pageable pageable){
        Page<OrdersLine> page = ordersLineRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(ordersLineMapper::toDto));
    }

    @Override
    public List<OrdersLineDto> queryAll(OrdersLineQueryCriteria criteria){
        return ordersLineMapper.toDto(ordersLineRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public OrdersLineDto findById(Long oddId) {
        OrdersLine ordersLine = ordersLineRepository.findById(oddId).orElseGet(OrdersLine::new);
        ValidationUtil.isNull(ordersLine.getOddId(),"OrdersLine","oddId",oddId);
        return ordersLineMapper.toDto(ordersLine);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public OrdersLineDto create(OrdersLine resources) {
        return ordersLineMapper.toDto(ordersLineRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(OrdersLine resources) {
        OrdersLine ordersLine = ordersLineRepository.findById(resources.getOddId()).orElseGet(OrdersLine::new);
        ValidationUtil.isNull( ordersLine.getOddId(),"OrdersLine","id",resources.getOddId());
        ordersLine.copy(resources);
        ordersLineRepository.save(ordersLine);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long oddId : ids) {
            ordersLineRepository.deleteById(oddId);
        }
    }

    @Override
    public void download(List<OrdersLineDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (OrdersLineDto ordersLine : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("订单编号", ordersLine.getOddOrderId());
            map.put("产品编号", ordersLine.getOddProdId());
            map.put("数量", ordersLine.getOddCount());
            map.put("单位", ordersLine.getOddUnit());
            map.put("产品单价", ordersLine.getOddPrice());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}